home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / tnserv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  2.7 KB  |  112 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "timer.h"
  5. #include "icmp.h"
  6. #include "netuser.h"
  7. #include "tcp.h"
  8. #include "telnet.h"
  9. #include "session.h"
  10. #include "ftp.h"
  11. #include "iface.h"
  12. #include "ax25.h"
  13. #include "lapb.h"
  14. #include "finger.h"
  15. #include "nr4.h"
  16.  
  17. struct tcb *tnet_tcb;
  18. tn1(argc,argv)
  19. char *argv[];
  20. {
  21.     struct socket lsocket;
  22.     extern int32 ip_addr;
  23.     void tnet_state();
  24.     void t_state(),rcv_char();
  25.  
  26.     /* Incoming Telnet */
  27.     lsocket.address = ip_addr;
  28.     if(argc < 2)
  29.         lsocket.port = TELNET_PORT;
  30.     else
  31.         lsocket.port = atoi(argv[1]);
  32.     tnet_tcb = open_tcp(&lsocket,NULLSOCK,TCP_SERVER,0,rcv_char,NULLVFP,tnet_state,0,(char *)NULL);
  33. }
  34. /* Handle incoming Telnet connect requests by creating a Telnet session,
  35.  * then change upcall vector so it behaves like an ordinary Telnet session.
  36.  * 
  37.  */
  38. static void
  39. tnet_state(tcb,old,new)
  40. struct tcb *tcb;
  41. char old,new;
  42. {
  43.     struct telnet *tn;
  44.     struct session *s,*newsession();
  45.     void t_state();
  46.     char *a;
  47.  
  48.     switch(new){
  49.     case ESTABLISHED:
  50.         log(tcb,"open Telnet");
  51.         /* Allocate a session descriptor */
  52.         if((s = newsession()) == NULLSESSION){
  53.             printf("\007Incoming Telnet call from %s refused; too many sessions\n",
  54.              psocket(&tcb->conn.remote));
  55.             fflush(stdout);
  56.             sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  57.             close_tcp(tcb);
  58.             return;
  59.         }
  60.         a = inet_ntoa(tcb->conn.remote.address);
  61.         if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  62.             strcpy(s->name,a);
  63.         s->type = TELNET;
  64.         s->parse = send_tel;
  65.         /* Create and initialize a Telnet protocol descriptor */
  66.         if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN){
  67.             printf("\007Incoming Telnet call refused; no space\n");
  68.             fflush(stdout);
  69.             sndmsg(tcb,"Call rejected; no space on remote system\n");
  70.             close_tcp(tcb);
  71.             s->type = FREE;
  72.             return;
  73.         }
  74.         tn->session = s;    /* Upward pointer */
  75.         tn->state = TS_DATA;
  76.         s->cb.telnet = tn;    /* Downward pointer */
  77.  
  78.         tcb->user = (char *)tn;    /* Upward pointer */
  79.         tn->tcb = tcb;        /* Downward pointer */
  80.         printf("\007Incoming Telnet session %lu from %s\n",
  81.          (long)(s - sessions),psocket(&tcb->conn.remote));
  82.         fflush(stdout);
  83.         tcb->s_upcall = t_state;
  84.         return;
  85.     case CLOSED:
  86.         /* This will only happen if the connection closed before
  87.          * the session was set up, e.g., if we refused it because
  88.          * there were too many sessions, or if the server is being
  89.          * shut down.
  90.          */
  91.         if(tcb == tnet_tcb)
  92.             tnet_tcb = NULLTCB;
  93.         del_tcp(tcb);
  94.         break;
  95.     }
  96. }
  97. /* Shut down Telnet server */
  98. tn0()
  99. {
  100.     if(tnet_tcb != NULLTCB)
  101.         close_tcp(tnet_tcb);
  102. }
  103. sndmsg(tcb,msg)
  104. struct tcb *tcb;
  105. char *msg;
  106. {
  107.     struct mbuf *bp;
  108.  
  109.     bp = qdata(msg,(int16)strlen(msg));
  110.     send_tcp(tcb,bp);
  111. }
  112.